<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
/**
 * @Desc 随机字符串
 *
 * @param int $length 个数
 *
 * @return string
 */
function randomChar($length = 8)
{
    $char = '';
    $str = 'abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $max = strlen($str) - 1;
    for ($i = 0; $i < $length; $i++) {
        $char .= $str{random_int(0, $max)};
    }
    return $char;
}

/**
 * @Desc 获取随机0-9,a-Z的随机5个不重复的字符
 *
 * @param int $len 个数
 *
 * @return string
 */
function randomStr($len = 5)
{
    $number = range('0', '9');     //获取0-9的数据
    $lowercase = range('a', 'z'); //获取a-z的数据
    $uppercase = range('A', 'Z'); //获取A-Z的数据
    $arr = array_merge($number, $lowercase, $uppercase); //合并数组
    shuffle($arr); //打乱数组
    $arr_end = array_slice($arr, 0, $len); //取出从角标0开始,5个数组单元
    return implode('', $arr_end);
}

/**
 * @Desc 根据经纬度算距离，返回结果单位是公里，先纬度，后经度
 *
 * @param string $lat1 纬度1
 * @param string $lng1 经度1
 * @param string $lat2 纬度2
 * @param string $lng2 经度2
 *
 * @return string
 */
function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $EARTH_RADIUS = 6378.137;
    $radLat1 = rad($lat1);
    $radLat2 = rad($lat2);
    $a = $radLat1 - $radLat2;
    $b = rad($lng1) - rad($lng2);
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * $EARTH_RADIUS;
    $s = round($s * 10000) / 10000;
    $s = number_format($s, 2);
    return $s;
}

function rad($d)
{
    return $d * M_PI / 180.0;
}